package com.restfb.etl.persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;

import com.restfb.etl.mundo.Comment;
import com.restfb.etl.mundo.Usuario;
import com.restfb.etl.totto.Principal;

public class FactTableMonitoreo {
	
	
	
	private Connection connection;

	public FactTableMonitoreo( Connection nConnection) {
		connection = nConnection;

	}

	public void cargarHechos(List<Tripla> triplas) 
	{
		String insert="INSERT INTO HECHOS_MONITOREO_CAMPANAS (CAMPANA_KEY,USUARIO_KEY,REACCION_USUARIO_KEY,BUENO,MALO,NEUTRAL,DATE_KEY) VALUES( ?,?,?,?,?,?,?)";
		for(Tripla trip:triplas)
		{

			String idCamp=trip.getCampana().getId();
			String idUser=trip.getIdUser();
			String idUserResponse=trip.getIdUserResponse();
			Comment com=trip.getUserResponse();
			
			try {
			
			////////////////////////////////////
			
			String select="SELECT DATE_KEY FROM DIM_DATE ORDER BY DATE_KEY ";
			
			String insertdate = "INSERT INTO DIM_DATE ( DATE_KEY, DATE_NOMBRE, DATE_COMPLETA ) VALUES (?, ?, ?) ";
			
			
				Statement st=connection.createStatement();
				
				ResultSet res=st.executeQuery(select);
				int idDate=1;
				if(res.last())
				{
					idDate=res.getInt(1);
					idDate++;
				}
				res.close();
				
				PreparedStatement pstm =connection.prepareStatement(insertdate);
				


				Date fecha = new Date(com.time*1000);
				
				pstm.setInt(1, idDate);
				pstm.setString(2, fecha.toString() );
				pstm.setDate(3, fecha);
				
				pstm.executeUpdate();
				
				pstm.close();
			
			///////////////////////////////////

			String resp=Principal.analizarTextoEnBuscaDeBuenoMaloNeutral(trip.getUserResponse().text);
			
				PreparedStatement ps= connection.prepareStatement(insert);
				if(resp.equals(Principal.BUENO))
				{
					System.out.println("Hola aqui :---------- "+idCamp);
					ps.setInt(1, Integer.parseInt(idCamp.replace("\"", " ")));
					ps.setInt(2, Integer.parseInt(idUser.replace("\"", " ")));
					ps.setInt(3, Integer.parseInt(idUserResponse.replace("\"", " ")));
					ps.setString(4, "1");
					ps.setString(5, "0");
					ps.setString(6, "0");
					ps.setInt(7,idDate);
					
				}
				else if(resp.equals(Principal.MALO))
				{
					System.out.println("Hola aqui :---------- "+idCamp);
					ps.setInt(1, Integer.parseInt(idCamp.replace("\"", " ")));
					ps.setInt(2, Integer.parseInt(idUser.replace("\"", " ")));
					ps.setInt(3, Integer.parseInt(idUserResponse.replace("\"", " ")));
					ps.setString(4, "0");
					ps.setString(5, "1");
					ps.setString(6, "0");
					ps.setInt(7,idDate);
					
				}
				else
				{
					System.out.println("Hola aqui :---------- "+idCamp);
					ps.setInt(1, Integer.parseInt(idCamp.replace("\"", " ")));
					ps.setInt(2, Integer.parseInt(idUser.replace("\"", " ")));
					ps.setInt(3, Integer.parseInt(idUserResponse.replace("\"", " ")));
					ps.setString(4, "0");
					ps.setString(5, "0");
					ps.setString(6, "1");
					ps.setInt(7,idDate);
					
				}
			ps.executeUpdate();
			ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
				
			
		}
	}

}
